package org.zakariya.flyoutmenudemo.menus;


import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.agp.utils.RectFloat;
import ohos.app.Context;
import org.zakariya.flyoutmenu.ColorUtils;
import org.zakariya.flyoutmenu.FlyoutMenuView;

public class PaletteFlyoutMenu {  //颜色选择器


    public static class ButtonRenderer extends FlyoutMenuView.ButtonRenderer {

        Paint paint;
        RectFloat insetButtonBounds = new RectFloat();
        float inset;

        int currentColor;

        double currentColorLuminance;

        public ButtonRenderer(float inset) {
            super();
            paint = new Paint();
            paint.setAntiAlias(true);
            this.inset = inset;
        }

        @SuppressWarnings("unused")
        public

        int getCurrentColor() {
            return currentColor;
        }

        public void setCurrentColor(int currentColor) {
            this.currentColor = currentColor;
            currentColorLuminance = 0.5;//ColorUtils.calculateLuminance(this.currentColor);  //判断颜色是否是亮色
        }

        @Override
        public void onDrawButtonContent(Canvas canvas, RectFloat buttonBounds, Color buttonColor, float alpha) {
            insetButtonBounds.left = buttonBounds.left + inset;
            insetButtonBounds.top = buttonBounds.top + inset;
            insetButtonBounds.right = buttonBounds.right - inset;
            insetButtonBounds.bottom = buttonBounds.bottom - inset;

            paint.setAlpha((int) (alpha * 255f));
            paint.setColor(new Color(currentColor));  //展示内容的颜色修改了
            paint.setStyle(Paint.Style.FILL_STYLE);
            canvas.drawOval(insetButtonBounds, paint);
            canvas.drawCircle(insetButtonBounds.getCenter().getPointX(),insetButtonBounds.getCenter().getPointY(),70,paint);  //中心点正确

            if (currentColorLuminance > 0.7) {
                paint.setStyle(Paint.Style.STROKE_STYLE);
                paint.setColor(new Color(0x33000000));
                canvas.drawOval(insetButtonBounds, paint);  //drawOval
                canvas.drawCircle(insetButtonBounds.getCenter().getPointX(),insetButtonBounds.getCenter().getPointY(),10,paint);
            }
        }

    }

    public static class MenuItem extends FlyoutMenuView.MenuItem {

        int color;
        int row;//行
        int cols;

        Paint paint;
        double cornerRadius = 0;

        public MenuItem(Context context, int id, int color, double cornerRadius, int positionx,int positopny) {
            super(context,id);
            this.color = ColorUtils.setAlphaComponent(color, 255);;//ColorUtils.setAlphaComponent(color, 255);
            this.cornerRadius = cornerRadius;
            paint = new Paint();
            paint.setAntiAlias(true);
            paint.setStyle(Paint.Style.FILL_STYLE);
            paint.setColor(new Color(color));
            this.row = positionx;
            this.cols = positopny;
        }


        @Override
        public void onDraw(Canvas canvas, RectFloat bounds, float degreeSelected) {
            super.onDraw(canvas, bounds, degreeSelected);
            RectFloat rectFloat = new RectFloat(100+cols*100,100+row*100,200+cols*100,200+100*row);
            if (cornerRadius > 0) {
                canvas.drawRoundRect(bounds, (float) cornerRadius, (float)cornerRadius, paint);
//                canvas.drawCircle(rectFloat.getCenter().getPointX(),rectFloat.getCenter().getPointY(),10,paint);
            } else {
                canvas.drawRect(bounds, paint);
            }
        }

        public int getColor() {
            return color;
        }

        public Paint getPaint() {
            return paint;
        }

        public double getCornerRadius() {
            return cornerRadius;
        }
    }
}
